import 'package:bloc/bloc.dart';
import 'package:dio/dio.dart';
import 'package:today_app/bloc/weather_today/weather_today_model.dart';

import 'weather_today_event.dart';
import 'weather_today_state.dart';

class WeatherTodayBloc extends Bloc<WeatherTodayEvent, WeatherTodayState> {
  WeatherTodayBloc() : super(WeatherTodayState().init()) {
    on<InitEvent>(_init);
  }

  void _init(InitEvent event, Emitter<WeatherTodayState> emit) async {
    /**
     * {
        "reason": "查询成功!",
        "result": {
        "city": "南京",
        "realtime": {
        "temperature": "3",
        "humidity": "57",
        "info": "晴",
        "wid": "00",
        "direct": "东风",
        "power": "2级",
        "aqi": "63"
        },
        "future": [{
        "date": "2023-02-27",
        "temperature": "2\/13℃",
        "weather": "晴转多云",
        "wid": {
        "day": "00",
        "night": "01"
        },
        "direct": "东南风"
        }, {
        "date": "2023-02-28",
        "temperature": "5\/16℃",
        "weather": "多云",
        "wid": {
        "day": "01",
        "night": "01"
        },
        "direct": "西风转北风"
        }, {
        "date": "2023-03-01",
        "temperature": "3\/16℃",
        "weather": "多云",
        "wid": {
        "day": "01",
        "night": "01"
        },
        "direct": "北风转东北风"
        }, {
        "date": "2023-03-02",
        "temperature": "5\/14℃",
        "weather": "多云",
        "wid": {
        "day": "01",
        "night": "01"
        },
        "direct": "东风转东南风"
        }, {
        "date": "2023-03-03",
        "temperature": "3\/14℃",
        "weather": "多云",
        "wid": {
        "day": "01",
        "night": "01"
        },
        "direct": "东南风"
        }]
        },
        "error_code": 0
        }
     */
    // https://apis.juhe.cn/simpleWeather/query?city=%E5%8D%97%E4%BA%AC&key=522d1260756a9933b9d67f5bac250c83
    Response response = await Dio().get("https://apis.juhe.cn/simpleWeather/query?city=%E5%8D%97%E4%BA%AC&key=522d1260756a9933b9d67f5bac250c83");
    WeatherTodayModel model = WeatherTodayModel.fromJson(response.data);
    emit(state.setModel(model));
  }
}
